﻿using CommonLibrary;
using CommonLibrary.Models;
using CommonSite.Areas.API.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLToolkit.Data.Linq;
using CommonSite.Common;
using CommonHelper;

namespace CommonSite.Areas.API.Controllers
{
    public class RoleController : BaseAPIController
    {
        public ActionResult Query()
        {
            using (CommonDatabase db = new CommonDatabase())
            {
                var query = (from p in db.sys_Role
                             select new Role
                             {
                                 Id = CryptHelper.Encrypt(p.Id),
                                 RoleName = p.Name
                             }).ToList();

                return Success(new
                {
                    Total = query.Count,
                    Rows = query
                });
            }
        }

        public ActionResult Create(string RoleName)
        {
            if (!RoleName.IsNullOrWhiteSpace())
            {
                try
                {
                    using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                    {
                        db.BeginTransaction();
                        if (db.sys_Role.Where(k => k.Name == RoleName)
                              .Count() > 0)
                        {
                            throw new ArgumentException("角色名称不能重复");
                        }

                        db.sys_Role.Insert(() => new sys_Role
                        {
                            Name = RoleName
                        });

                        db.CommitTransaction();
                        return Success();
                    }
                }
                catch (Exception ex)
                {
                    return Failed(ex.Message);
                }
            }

            return ArgumentFailed();
        }

        public ActionResult Update(Role role)
        {
            if (this.ModelState.IsValid)
            {
                try
                {
                    using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                    {
                        db.BeginTransaction();
                        int id = CryptHelper.DecryptToInt32(role.Id);
                        db.sys_Role.Where(k => k.Id == id)
                          .Set(k => k.Name, role.RoleName)
                          .Update();

                        db.CommitTransaction();
                        return Success();
                    }
                }
                catch (Exception ex)
                {
                    return Failed(ex.Message);
                }
            }

            return ArgumentFailed();
        }

        public ActionResult Delete(Role role)
        {
            try
            {
                using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                {
                    db.BeginTransaction();
                    int id = CryptHelper.DecryptToInt32(role.Id);
                    if (id == 1)
                    {
                        throw new ArgumentException("超级管理员角色无法删除");
                    }

                    if (db.sys_Role.Where(k => k.Id == id && role.RoleName == k.Name)
                          .Count() == 0)
                    {
                        throw new ArgumentException("角色不存在");
                    }

                    db.sys_RoleMenu.Where(k => k.RoleId == id)
                      .Delete();
                    db.sys_UserRole.Where(k => k.RoleId == id)
                      .Delete();
                    db.sys_RolePermission.Where(k => k.RoleId == id)
                      .Delete();
                    db.sys_Role.Where(k => k.Id == id)
                      .Delete();

                    db.CommitTransaction();
                    return Success();
                }
            }
            catch (Exception ex)
            {
                return Failed(ex.Message);
            }
        }
    }
}
